/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package Pokemons;

import java.awt.Graphics;
import java.awt.Graphics2D;
import java.awt.Image;
import java.awt.Rectangle;
import java.awt.event.KeyEvent;
import java.util.ArrayList;
import java.util.Random;
import javax.swing.ImageIcon;
import javax.swing.JPanel;
import Missiles.Missile;
import Controller.Table;

/**
 * Autores:
 * Hugo Hiroshi Yamamura
 * Marina Coimbra Viviani
 * 
 * Classe Pokemon
 * Classe Pai de todos os pokemons
 */
public class Pokemon {
    
    private String pokemon;
    private String ballName;
   
    private int dx;
    private int dy;
    private int x;
    private int y;
    private int width;
    private int height;
    private boolean visible;
    private Image image
            ;
    private int imagew;
    private int imagex;
    
    
    private ArrayList missiles;
    private int imagex1;
    private int control;
    
    private int type; 
    
    /*Type Explanation - Elemental
     * 1 - Water - Good Against Fire
     * 2 - Fire - Good Against Plant
     * 3 - Plant - Good Against Water
     */
   
  /**
 * Construtor: Pokemon
 * Constroi novo pokemon.
 */    
       
    public Pokemon(){
        pokemon = new String();
        control = 1;
        Table table = new Table();
        Random rand = new Random();
        ImageIcon ii = new ImageIcon(this.getClass().getResource(pokemon));
        image = ii.getImage();
        width = image.getWidth(null);
        height = image.getHeight(null);
        missiles = new ArrayList();
        visible = true;
        imagew = 30;
        imagex = 0;
    }
    
     /*
     * Método move
     * @param times : eh um desacelerador da função baseado em quantas vezes foi rodado o cronometro
     * Funcionamento: Anima a imagem, impede o pokemon de andar para fora da tabela e faz o pokemon andar
     */
    public void move(int times){
        
        Table table = new Table();
        
       if(times % 20 == 0) 
        if(imagex1 != imagew)
            imagex1 += imagew;
        else imagex1 = 0;
        
        x = x + dx;
        y = y + dy;

        if (x < table.getXBEGIN()) {
            x = table.getXBEGIN();
        }

        if (y < table.getYBEGIN()) {
            y = table.getYBEGIN();
        }
        
        if(x > table.getXEND())
            x = table.getXEND();
        if(y > table.getYEND())
            y = table.getYEND();
    }
    
     /*
    * 
    * Metodo: paint
    * Funcionamento: pinta o pokemon no JPanel.
    */    
    public void paint(Graphics g, JPanel panel){
        if(this.isVisible()){
            Graphics2D g2d = (Graphics2D)g;
            g2d.drawImage(this.getImage(), this.getX(), this.getY(), this.getX()+ this.getImagew(), this.getY() + this.getHeight(), this.getImagex1(), 0, this.getImagex1()+ this.getImagew(), this.getHeight(), panel);
        }
    }
            
    
     /*
    *
    * Metodo: fire
    * lança misseis
    */    
    public void fire() {
        missiles.add(new Missile(x + width, y + height/2));
    }
  /*
     * Metodo: KeyPressed
     * @param evento de teclado
     * Funcionamento: determina o que deve ser feito a partir de eventos de teclado
     * Tem um controle pra não poder apertar infinito a tecla.
     * 
     */
    public void keyPressed(KeyEvent e) {
    
        int key = e.getKeyCode();
        
        
        if( control == 1){
        
            if (key == KeyEvent.VK_SPACE) {
                fire();
            }

            if (key == KeyEvent.VK_LEFT) {
                dx = -3;
            }

            if (key == KeyEvent.VK_RIGHT) {
                dx = 3;
            }

            if (key == KeyEvent.VK_UP) {
                dy = -2;
            }

            if (key == KeyEvent.VK_DOWN) {

            dy = 2;
            }
            
           
        }
        
        if(control == 0){
            if (key == KeyEvent.VK_LEFT) {
            dx = 0;
        }

        if (key == KeyEvent.VK_RIGHT) {
            dx = 0;
        }

        if (key == KeyEvent.VK_UP) {
            dy = 0;
        }

        if (key == KeyEvent.VK_DOWN) {
            dy = 0;
        }
        }
        
        control = 0;
        
        

    }
    
    
      /*
     * Método KeyReleased
     * @param evento de teclado. Tecla solta.
     * Zera o deslocamento dos pokemons. Com teclas pressionadas não há deslocamento.
     * 
     * 
     */
    
    public void keyReleased(KeyEvent e) {
        int key = e.getKeyCode();

        control = 1;
        
        if (key == KeyEvent.VK_LEFT) {
            dx = 0;
        }

        if (key == KeyEvent.VK_RIGHT) {
            dx = 0;
        }

        if (key == KeyEvent.VK_UP) {
            dy = 0;
        }

        if (key == KeyEvent.VK_DOWN) {
            dy = 0;
        }
    }

    /*
    * 
    * Metodo: getBounds
    * Funcionamento: Cria um retangulo em torno da figura para colisao.
     * @return o retangulo
    */    
    public Rectangle getBounds() {
        return new Rectangle(this.getX() + this.getImagex(), this.getY(), this.getImagew(), this.getHeight());
    }

    
    
      /*
    * --------------------------
    *  Getters e Setters 
    * -------------------------
    */       
    public int getDx() {
        return dx;
    }

    public void setDx(int dx) {
        this.dx = dx;
    }

    public int getDy() {
        return dy;
    }

    public void setDy(int dy) {
        this.dy = dy;
    }

    public int getHeight() {
        return height;
    }

    public void setHeight(int height) {
        this.height = height;
    }

    public Image getImage() {
        return image;
    }

    public void setImage(Image image) {
        this.image = image;
    }

  
    

    public ArrayList getMissiles() {
        return missiles;
    }

    public void setMissiles(ArrayList missiles) {
        this.missiles = missiles;
    }

    public String getPokemon() {
        return pokemon;
    }

    public void setPokemon(String pokemon) {
        this.pokemon = pokemon;
    }

    public boolean isVisible() {
        return visible;
    }

    public void setVisible(boolean visible) {
        this.visible = visible;
    }

    public int getWidth() {
        return width;
    }

    public void setWidth(int width) {
        this.width = width;
    }

    public int getX() {
        return x;
    }

    public void setX(int x) {
        this.x = x;
    }

    public int getY() {
        return y;
    }

    public void setY(int y) {
        this.y = y;
    }

    public int getImagex1() {
        return imagex1;
    }

    public void setImagex1(int imagex1) {
        this.imagex1 = imagex1;
    }

    public int getImagew() {
        return imagew;
    }

    public void setImagew(int imagew) {
        this.imagew = imagew;
    }

    public int getImagex() {
        return imagex;
    }

    public void setImagex(int imagex) {
        this.imagex = imagex;
    }
        
    
}
